#!/bin/bash

# 配置变量
ZABBIX_VSERSION=4
MYSQL_ROOT_PASSWD=1234.ABCE
MYSQL_ZABBIX_PASSWD=1234.ABCE


# 优化ssh连接速度
sed -i "s/#UseDNS yes/UseDNS no/" /etc/ssh/sshd_config
sed -i "s/GSSAPIAuthentication .*/GSSAPIAuthentication no/" /etc/ssh/sshd_config
systemctl restart sshd

# 配置阿里云yum源
rm -f /etc/yum.repos.d/*.repo
curl -so /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
curl -so /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i '/aliyuncs.com/d' /etc/yum.repos.d/Centos-7.repo /etc/yum.repos.d/epel-7.repo

# 文件/进程 限制
if [ ! "$(grep '# My Limits' /etc/security/limits.conf)" ]; then
    echo -e "\n# My Limits" >> /etc/security/limits.conf
    echo "* soft nofile 65535" >> /etc/security/limits.conf
    echo "* hard nofile 65535" >> /etc/security/limits.conf
    echo "* soft nproc 65535"  >> /etc/security/limits.conf
    echo "* hard nproc 65535"  >> /etc/security/limits.conf
    echo "* soft  memlock  unlimited"  >> /etc/security/limits.conf
    echo "* hard memlock  unlimited"  >> /etc/security/limits.conf
fi

# 启用路由转发
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
echo 'net.bridge.bridge-nf-call-iptables = 1' >> /etc/sysctl.conf
echo 'net.bridge.bridge-nf-call-ip6tables = 1' >> /etc/sysctl.conf

# 同时同一用户可以监控的目录数量
echo 'fs.inotify.max_user_watches=524288' >> /etc/sysctl.conf

# 进程拥有VMA(虚拟内存区域)的数量
echo 'vm.max_map_count=655360' >> /etc/sysctl.conf

# TIME_WAIT
echo 'net.ipv4.tcp_syncookies = 1' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_tw_reuse = 1' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_tw_recycle = 1' >> /etc/sysctl.conf

# 立即生效配置
modprobe br_netfilter
sysctl -p -w /etc/sysctl.conf

# stop/disable selinux
setenforce 0
sed -i 's#SELINUX=.*#SELINUX=disabled#' /etc/selinux/config

# Install httpd
yum -y install httpd
rm -f /etc/httpd/conf.d/welcome.conf

# Configure httpd
echo -e '\nServerTokens Prod' >> /etc/httpd/conf/httpd.conf
echo 'KeepAlive On' >> /etc/httpd/conf/httpd.conf
sed -i '151 s#AllowOverride.*#AllowOverride All#' /etc/httpd/conf/httpd.conf
sed -i '164 s#DirectoryIndex.*#DirectoryIndex index.html index.cgi index.php#' /etc/httpd/conf/httpd.conf

# Start httpd service
systemctl start httpd
systemctl enable httpd

# If Firewalld is running, allow HTTP service. HTTP uses 80/TCP
firewall-cmd --add-service=http --permanent
firewall-cmd --reload

# Install PHP
yum -y install php php-mbstring php-pear

# Configure PHP timezone
sed -i 's#;date.timezone.*#date.timezone = "Asia/Shanghai"#' /etc/php.ini

# Restart httpd service
systemctl restart httpd

# Create php test file
cat > /var/www/html/index.php  <<-EOF
<html>
<meta http-equiv="refresh" content="1">
<body>
    <div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
    <?php print Date("Y-m-d h:i:sa"); ?>
    </div>
</body>
</html>
EOF

# Install mariadb
yum -y install mariadb-server

# add follows within [mysqld] section
echo -e "\n[mysqld]\ncharacter-set-server=utf8" >> /etc/my.cnf

# Start mariadb service
systemctl start mariadb
systemctl enable mariadb

# Install expect
if [ ! -e /usr/bin/expect ];then
    yum install expect -y
fi

# Initial Settings for MariaDB
echo "now let's begin mysql_secure_installation "
if [ ! -e /usr/bin/expect ]
 then  yum install expect -y
fi

# Mariadb init
# expect 方式
MYSQL_PASSWD=$MYSQL_ROOT_PASSWD
echo '#!/usr/bin/expect
set timeout 10
set password [lindex $argv 0]
spawn bash -c " mysql_secure_installation "
expect "Enter current password for root (enter for none): "
    send "\n"
expect "Set root password? "
    send "Y\n"
expect "New password:"
    send "$password\n"
expect "Re-enter new password:"
    send "$password\n"
expect "Remove anonymous users? "
    send "Y\n"
expect "Disallow root login remotely? "
    send "Y\n"
expect "Remove test database and access to it? "
    send "Y\n"
expect "Reload privilege tables now? "
    send "Y\n"
    send "\n"
    send "exit"
    send "\n"
expect eof' > mysql_secure_installation.exp
chmod +x mysql_secure_installation.exp
./mysql_secure_installation.exp $MYSQL_PASSWD

# echo 方式
# echo -e "\nY\n$MYSQL_PASSWD\n$MYSQL_PASSWD\nY\nn\nY\nY\n" | mysql_secure_installation

# If Firewalld is running and also MariaDB is used from remote Hosts, allow service like follows. MariaDB uses 3306/TCP.
firewall-cmd --add-service=mysql --permanent
firewall-cmd --reload

# Install some other required packages and Zabbix repository
yum -y install php-mysql php-gd php-xml php-bcmath
if [ $ZABBIX_VSERSION  -eq 3 ]; then 
    yum -y install http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-1.el7.centos.noarch.rpm
elif [ $ZABBIX_VSERSION  -eq 4 ]; then
    yum -y install https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-1.el7.noarch.rpm
fi

# 替换安装源
sed -i 's#repo.zabbix.com#mirrors.tuna.tsinghua.edu.cn/zabbix#g' /etc/yum.repos.d/zabbix.repo

# Install Zabbix server
yum -y install zabbix-get zabbix-server-mysql zabbix-web-mysql zabbix-agent

# Create a database for Zabbix
zabbix_pass=$MYSQL_ZABBIX_PASSWD
command="mysql -uroot -p$MYSQL_PASSWD"
echo 'create database zabbix;' | $command
echo "grant all privileges on zabbix.* to zabbix@'localhost' identified by '$zabbix_pass';" | $command
# echo "grant all privileges on zabbix.* to zabbix@'%' identified by '$zabbix_pass';" | $command
echo 'flush privileges;' | $command
cd /usr/share/doc/zabbix-server-mysql-*/
gunzip create.sql.gz
cat create.sql | $command zabbix

# Configure and start Zabbix Server.
sed -i 's/# DBHost.*/DBHost=localhost/' /etc/zabbix/zabbix_server.conf
sed -i "s/# DBPassword=/DBPassword=$zabbix_pass/" /etc/zabbix/zabbix_server.conf
sed -i '20a\        php_value date.timezone Asia/ShangHai' /etc/httpd/conf.d/zabbix.conf
systemctl start zabbix-server
systemctl enable zabbix-server
systemctl restart httpd

# If Firewalld is running, allow Zabbix related ports
firewall-cmd --add-service={http,https} --permanent
firewall-cmd --add-port={10051/tcp,10050/tcp} --permanent
firewall-cmd --reload

# start zabbix-agent
systemctl start zabbix-agent
systemctl enable zabbix-agent

############################ 图表字体乱码 #################################
# Windows  C:\Windows\Fonts  搜索 simkai.ttf
# mv /usr/share/zabbix/assets/fonts/SIMKAI.TTF /usr/share/zabbix/assets/fonts/simkai.ttf
# cp /usr/share/zabbix/include/defines.inc.php{,.bak}
# sed -i 's#graphfont#simkai#' /usr/share/zabbix/include/defines.inc.php
# 刷新浏览器

# 更换资源
if [ $ZABBIX_VSERSION  -eq 3 ]; then 
    curl -o /usr/share/zabbix/fonts/simkai.ttf http://dl.hc-yun.com/soft/simkai.ttf
elif [ $ZABBIX_VSERSION  -eq 4 ]; then
    curl -o /usr/share/zabbix/assets/fonts/simkai.ttf http://dl.hc-yun.com/soft/simkai.ttf
fi